package com.app.miwawa.api.eam.service.bo;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.query.Criterion.Operator;
import org.framework.common.api.query.Criterion.ValueType;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.dao.CommonDaoAware;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.ext.util.DateUtil;
import org.framework.common.ext.util.StringUtil;
import org.framework.common.ext.util.DateUtil.DateRange;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.eam.entity.ClassesRecord;
import com.app.miwawa.api.eam.entity.StudentCheck;
import com.app.miwawa.api.eam.entity.StudentLeave;
import com.app.miwawa.api.eam.entity.view.StudentLesson;
import com.app.miwawa.api.esm.entity.contract.Buyinfo;

@Service("studentBo")
public class StudentBo extends CommonBoAdapter<CommonEntityAware<String>, String> {

	@Resource(name = "defaultDao")
	private CommonDaoAware<CommonEntityAware<String>, String> dao;
	
	/**
	 * 签到.
	 * @param check
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月16日 下午11:10:06
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void check(final StudentCheck check) throws Exception {
		String studentid = check.getStudentId();
		String classesid = check.getClassesId();
		String contractid = check.getContractId();
		Date checkDate = check.getCheckDate();
		
		ProcessModel process = ProcessModel.factory();
		// 查询耗课数
		String useCount = process.jdbc.queryForObject(process.properties.getProperty("classes.select.usecount"),String.class,classesid);
		
		Float usecount = StringUtil.isNotEmpty(useCount)? Float.parseFloat(useCount):0;
		
		if(hasCheck(studentid, classesid, contractid, checkDate)){
			
			try{
				// 新增签到记录
				dao.insert(check);
				
				// 查询签到记录数
				long count = count(studentid, classesid, contractid);
				
				// 更新已用课时
				Buyinfo buyinfo = (Buyinfo)dao.loader((Class)Buyinfo.class, "contractId", contractid);
				buyinfo.setUseing(check.getItem(), count, false, usecount);
				dao.update(buyinfo);
			}catch(NullPointerException e){
				dao.remove(check);
				throw new Exception();
			}
		}
	}
	
	/**
	 * 请假.
	 * @param leave
	 * @author chanlong(陈龙)
	 * @throws Exception 
	 * @date 2016年6月4日 上午12:42:23
	 */
	public void leave(final StudentLeave leave) throws Exception {
		
		String studentid = leave.getStudentId();
		String classesid = leave.getClassesId();
		String contractid = leave.getContractId();
		Date leaveDate = leave.getLeaveDate();
		
		if(hasLeave(studentid, classesid, contractid, leaveDate)){
			dao.insert(leave);
		}
	}
	
	/**
	 * 取消签到.
	 * @param lesson
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午8:55:16
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void uncheck(final StudentLesson lesson) throws Exception{
		// 查询签到记录数
		long count = count(lesson.getStudentId(), lesson.getClassesId(), lesson.getContractId());
		
		// 更新已用课时
		String contractId = lesson.getContractId();
		String classesid = lesson.getClassesId();
		
		ProcessModel process = ProcessModel.factory();
		// 查询耗课数
		String useCount = process.jdbc.queryForObject(process.properties.getProperty("classes.select.usecount"),String.class,classesid);
		
		Float usecount = StringUtil.isNotEmpty(useCount)? Float.parseFloat(useCount):0;
		
		try{
			Buyinfo buyinfo = (Buyinfo)dao.loader((Class)Buyinfo.class, "contractId", contractId);
			buyinfo.setUseing(lesson.getItem(), count, true, usecount);
			dao.update(buyinfo);
			
			dao.remove(dao.loader((Class) StudentCheck.class, lesson.getCheckId()));
		}catch(NullPointerException e){
			throw new Exception();
		}
	}

	/**
	 * 取消请假.
	 * @param lesson
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月15日 下午8:55:05
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void unleave(final StudentLesson lesson) throws Exception {
		dao.remove(dao.loader((Class) StudentLeave.class, lesson.getCheckId()));
	}
	
	/*
	 * 查询签到记录数.
	 * @date 2016年4月15日 下午8:52:33
	 */
	private long count(final String studentId, final String classesId, final String contractId) throws Exception{
		QueryModel query = new QueryModel(StudentCheck.class, 0, 0);
		query.addAndQuery("studentId", studentId, Operator.EQ);
		query.addAndQuery("classesId", classesId, Operator.EQ);
		query.addAndQuery("contractId", contractId, Operator.EQ);
		query.addAndQuery("checkDate", DateUtil.range(DateRange.MONTH, new Date()), Operator.BETWEEN, ValueType.DATES);
		return dao.count(query);
	}
	
	/*
	 * 是否已签到.
	 * @date 2016年4月15日 下午8:52:33
	 */
	private boolean hasCheck(final String studentId, final String classesId, final String contractId, final Date checkDate) throws Exception{
		new ClassesRecord(checkDate, classesId).save((Map<String, Object>)null);
		
		QueryModel query = new QueryModel(StudentCheck.class, 0, 0);
		query.addAndQuery("studentId", studentId, Operator.EQ);
		query.addAndQuery("classesId", classesId, Operator.EQ);
		query.addAndQuery("contractId", contractId, Operator.EQ);
		query.addAndQuery("checkDate", checkDate, Operator.EQ, ValueType.DATES);
		return dao.count(query) > 0 ? false : true;
	}
	
	/*
	 * 是否已请假.
	 * @date 2016年4月15日 下午8:52:33
	 */
	private boolean hasLeave(final String studentId, final String classesId, final String contractId, final Date leaveDate) throws Exception{
		new ClassesRecord(leaveDate, classesId).save((Map<String, Object>)null);
		
		QueryModel query = new QueryModel(StudentLeave.class, 0, 0);
		query.addAndQuery("studentId", studentId, Operator.EQ);
		query.addAndQuery("classesId", classesId, Operator.EQ);
		query.addAndQuery("contractId", contractId, Operator.EQ);
		query.addAndQuery("leaveDate", leaveDate, Operator.EQ, ValueType.DATES);
		return dao.count(query) > 0 ? false : true;
	}
}
